package cn.csg.iotplatform.module.system.service.testingmanufacturer;


import cn.csg.iotplatform.framework.common.pojo.PageResult;
import cn.csg.iotplatform.framework.common.util.object.BeanUtils;
import cn.csg.iotplatform.module.system.controller.admin.testingmanufacturer.vo.DvTestingManufacturerPageReqVO;
import cn.csg.iotplatform.module.system.controller.admin.testingmanufacturer.vo.DvTestingManufacturerSaveReqVO;
import cn.csg.iotplatform.module.system.dal.dataobject.testingmanufacturer.DvTestingManufacturerDO;
import cn.csg.iotplatform.module.system.dal.mysql.testingmanufacturer.DvTestingManufacturerMapper;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jodd.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.util.Arrays;
import java.util.List;

import static cn.csg.iotplatform.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 检测机构信息 Service 实现类
 *
 * @author 系统管理员1111
 */
@Service
@Validated
public class DvTestingManufacturerServiceImpl implements DvTestingManufacturerService {

    @Resource
    private DvTestingManufacturerMapper dvTestingManufacturerMapper;

    @Override
    public String createDvTestingManufacturer(DvTestingManufacturerSaveReqVO createReqVO) {
        // 插入
        DvTestingManufacturerDO dvTestingManufacturer = BeanUtils.toBean(createReqVO, DvTestingManufacturerDO.class);

        //校验机构名称：必填，系统唯一
        if (StringUtil.isNotEmpty( dvTestingManufacturer.getManufacturerName() )){

            if ( dvTestingManufacturerMapper
                    .selectCount(new LambdaQueryWrapper<DvTestingManufacturerDO>()
                            .eq(DvTestingManufacturerDO::getManufacturerName, dvTestingManufacturer.getManufacturerName())
                            .eq(DvTestingManufacturerDO::getDeleted, 0)) > 0 ){
                throw exception( "机构名称已存在，请勿重复提交" );
            }
        }else {
            throw exception( "机构名称不能为空" );
        }

        //校验机构标识码：必填，用于唯一标识一个机构，系统内唯一
        if (StringUtil.isNotEmpty( dvTestingManufacturer.getManufacturerNo() )){

            if ( dvTestingManufacturerMapper
                    .selectCount(new LambdaQueryWrapper<DvTestingManufacturerDO>()
                            .eq(DvTestingManufacturerDO::getManufacturerNo, dvTestingManufacturer.getManufacturerNo())
                            .eq(DvTestingManufacturerDO::getDeleted, 0)) > 0 ){
                throw exception( "机构标识码已存在，请勿重复提交" );
            }
        }else {
            throw exception( "机构标识码不能为空" );
        }

        dvTestingManufacturerMapper.insert(dvTestingManufacturer);
        // 返回
        return dvTestingManufacturer.getId();
    }

    @Override
    public void updateDvTestingManufacturer(DvTestingManufacturerSaveReqVO updateReqVO) {
        // 校验存在
        validateDvTestingManufacturerExists(updateReqVO.getId());

        // 更新
        DvTestingManufacturerDO updateObj = BeanUtils.toBean(updateReqVO, DvTestingManufacturerDO.class);

        //校验机构名称：必填，系统唯一
        if (StringUtil.isNotEmpty( updateObj.getManufacturerName() )){

            if ( dvTestingManufacturerMapper
                    .selectCount(new LambdaQueryWrapper<DvTestingManufacturerDO>()
                            .eq(DvTestingManufacturerDO::getManufacturerName, updateObj.getManufacturerName())
                            .ne(DvTestingManufacturerDO::getId, updateObj.getId())
                            .eq(DvTestingManufacturerDO::getDeleted, 0)) > 0 ){
                throw exception( "机构名称已存在，请勿重复提交" );
            }
        }else {
            throw exception( "机构名称不能为空" );
        }

        //校验机构标识码：必填，用于唯一标识一个机构，系统内唯一
        if (StringUtil.isNotEmpty( updateObj.getManufacturerNo() )){

            if ( dvTestingManufacturerMapper
                    .selectCount(new LambdaQueryWrapper<DvTestingManufacturerDO>()
                            .eq(DvTestingManufacturerDO::getManufacturerNo, updateObj.getManufacturerNo())
                            .ne(DvTestingManufacturerDO::getId, updateObj.getId())
                            .eq(DvTestingManufacturerDO::getDeleted, 0)) > 0 ){
                throw exception( "机构标识码已存在，请勿重复提交" );
            }
        }else {
            throw exception( "机构标识码不能为空" );
        }

        dvTestingManufacturerMapper.updateById(updateObj);
    }

    @Override
    public void deleteDvTestingManufacturer(String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        //分批删除
        int batchSize = 100;
        int total = idList.size();
        int count = 0;
        while (count < total) {
            int endIndex = Math.min(count + batchSize, total);
            List<String> subList = idList.subList(count, endIndex);
            dvTestingManufacturerMapper.deleteBatchIds(subList);
            count += batchSize;
        }
    }

    private void validateDvTestingManufacturerExists(String id) {
        // if (dvTestingManufacturerMapper.selectById(id) == null) {
        //     throw exception(DV_TESTING_MANUFACTURER_NOT_EXISTS);
        // }
    }

    @Override
    public DvTestingManufacturerDO getDvTestingManufacturer(String id) {
        return dvTestingManufacturerMapper.selectById(id);
    }

    /**
     * 检测机构分页
     * @param pageReqVO
     * @return
     */
    @Override
    public PageResult<DvTestingManufacturerDO> getDvTestingManufacturerPage(DvTestingManufacturerPageReqVO pageReqVO) {



        return dvTestingManufacturerMapper.selectPage(pageReqVO);
    }

    // @Override
    // public PageResult<DvTestingManufacturerDO> getDvTestingManufacturerPage(DvTestingManufacturerPageReqVO pageReqVO) {
    //     return dvTestingManufacturerMapper.selectPage(pageReqVO);
    // }

}